package com.yinhai.msg.email.tool;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @ClassName JavaBeanUtil
 * @Description: JavaBean工具类
 * @Company: com.yinhai
 * @author huangyh
 * @date 2013-10-8
 * @version 1.0
 */
public class JavaBeanUtil {
	/**
	 * 检测JavaBean对象指定属性是否存在空属性值
	 * 
	 * @param javaBean
	 * @param attributeNames
	 *            未设置则检测所有的属性值
	 * @throws Exception
	 */
	public static void checkJavaBean(Object javaBean, String[] attributeNames)
			throws Exception {
		Method[] methods = javaBean.getClass().getMethods();
		boolean isSetAttributeNames = (attributeNames == null || attributeNames.length == 0) ? false
				: true;
		if (isSetAttributeNames) {
			for (String setAttributeName : attributeNames) {
				for (int i = 0; i < methods.length; i++) {
					Method method = methods[i];
					if (method.getName().startsWith("get")) {
						String attributeName = method.getName().substring(3)
								.toLowerCase();// Checked
						if (setAttributeName.toLowerCase()
								.equals(attributeName)
								&& ObjectUtil.isEmpty(method.invoke(javaBean))) {
							throw new IllegalArgumentException("the attribute "
									+ attributeName + " value of "
									+ javaBean.getClass().getName()
									+ " is null");
						}
					}
				}
			}
		} else {
			for (int i = 0; i < methods.length; i++) {
				Method method = methods[i];
				if (method.getName().startsWith("get")) {
					String attributeName = method.getName().substring(3)
							.toLowerCase();// Checked
					if (ObjectUtil.isEmpty(method.invoke(javaBean))) {
						throw new IllegalArgumentException("the attribute "
								+ attributeName + " value of "
								+ javaBean.getClass().getName() + " is null");
					}
				}
			}
		}
	}

	/**
	 * 检测JavaBean对象指定属性是否存在空属性值
	 * 
	 * @param javaBean
	 * @param includeAttributeNames
	 * @param exceptAttributeNames
	 * @throws Exception
	 */
	public static void checkJavaBean(Object javaBean,
			String[] includeAttributeNames, String[] exceptAttributeNames)
			throws Exception {
		Method[] methods = javaBean.getClass().getMethods();
		List<String> includeList = null, exceptList = null, allAttributeList = null, checkAttributeList = null;
		if (includeAttributeNames != null && includeAttributeNames.length > 0) {
			includeList = new ArrayList<String>();
			Collections.addAll(includeList, includeAttributeNames);
		}
		if (exceptAttributeNames != null && exceptAttributeNames.length > 0) {
			exceptList = new ArrayList<String>();
			Collections.addAll(exceptList, exceptAttributeNames);
		}
		for (int i = 0; i < methods.length; i++) {
			if (allAttributeList == null) {
				allAttributeList = new ArrayList<String>();
			}
			Method method = methods[i];
			if (method.getName().startsWith("get")) {
				allAttributeList.add(method.getName().substring(3)
						.toLowerCase());
			}
		}
		checkAttributeList = new ArrayList<String>();
		if (includeList != null && includeList.size() > 0) {
			includeList.removeAll(exceptList);
			checkAttributeList.addAll(includeList);
		} else {
			allAttributeList.removeAll(exceptList);
			checkAttributeList.addAll(allAttributeList);
		}
		for (int i = 0; i < methods.length; i++) {
			Method method = methods[i];
			if (method.getName().startsWith("get")) {
				String attributeName = method.getName().substring(3)
						.toLowerCase();// Checked
				if (checkAttributeList.contains(attributeName)
						&& ObjectUtil.isEmpty(method.invoke(javaBean))) {
					throw new IllegalArgumentException("the attribute "
							+ attributeName + " value of "
							+ javaBean.getClass().getName() + " is null");
				}
			}
		}
	}
}
